package bank;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class TransferMoney extends JPanel implements ActionListener {
    private Atm frame;
    private Icon btnOff = Util.getIcon("/images/buttonOff.jpg");
    private Icon btnSure = Util.getIcon("/images/buttonSure.jpg");
    private JButton imageBtn1 = new JButton(btnOff);
    private JButton imageBtn2 = new JButton(btnSure);
    private JTextField nameText = new JTextField();
    private JTextField moneyText = new JTextField();
    public TransferMoney(Atm frame){
        this.frame = frame;
        this.setLayout(null);
        this.setOpaque(false);

        JLabel acc1 = new JLabel("请输入转账账户:");
        acc1.setFont(new Font("宋体", Font.PLAIN, 20));
        acc1.setBounds(200, 200, 300, 50);
        this.add(acc1);
        JLabel acc = new JLabel("请输入转账金额:");
        acc.setFont(new Font("宋体", Font.PLAIN, 20));
        acc.setBounds(200, 300, 300, 50);
        this.add(acc);

        nameText.setBounds(400, 200, 300, 50);
        nameText.setFont(new Font("宋体", Font.PLAIN, 20));
        this.add(nameText);
        moneyText.setBounds(400, 300, 300, 50);
        moneyText.setFont(new Font("宋体", Font.PLAIN, 20));
        this.add(moneyText);


        imageBtn1.setBounds(650, 430, 140, 50);
        this.add(imageBtn1);
        imageBtn1.addActionListener(this);
        imageBtn2.setBounds(300, 430, 140, 50);
        this.add(imageBtn2);
        imageBtn2.addActionListener(this);

    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == imageBtn1) {
            frame.switchPanel("menu");
        } else if (e.getSource() == imageBtn2) {
            if (!moneyText.getText().matches("^[0-9]+(.[0-9]{1,2})?$")){
                JOptionPane.showMessageDialog(this, "错误代码");
                return;
            }
            Double m = Double.parseDouble(moneyText.getText());

            if (frame.getUsers().get(frame.getIndex()).getAccount().equals(nameText.getText())){
                JOptionPane.showMessageDialog(this, "不能给自己转账");
                return;
            }
            for (User obj : frame.getUsers()) {
                if (nameText.getText().equals(obj.getAccount())) {

                    if (Double.parseDouble(moneyText.getText()) < frame.getUsers().get(frame.getIndex()).getBalance()) {
                        obj.setBalance(obj.getBalance() + m);
                        frame.getUsers().get(frame.getIndex()).setBalance(frame.getUsers().get(frame.getIndex()).getBalance() - m);
                        String sql="UPDATE t_users SET u_money="+frame.getUsers().get(frame.getIndex()).getBalance()+" where u_name='"+frame.getUsers().get(frame.getIndex()).getAccount()+"'";
                        Util.insert(sql);
                        String sql2="UPDATE t_users SET u_money="+obj.getBalance()+" where u_name='"+obj.getAccount()+"'";
                        Util.insert(sql2);
                        JOptionPane.showMessageDialog(this, "转账成功");
                    } else {
                        JOptionPane.showMessageDialog(this, "余额不足不能转账");
                    }
                    return;
                }
            }
            JOptionPane.showMessageDialog(this, "查无次账户");
        }
    }
    }

